var Protomean_Gecko = Class.create();

Protomean_Gecko.prototype = {

	initialize : function() {
	
		this._nl = "\n";
		
	}
,
	initIframe : function(iframe) {
		this._iframe = iframe;
		this._doc = iframe.contentDocument;

		this._doc.title = this._index;

		this.skin();

		//init html value
		this._doc.body.innerHTML = this._el.value || this._opts.html;
		
		//init designMode
		this.enableDesignMode();

		//pre-bind functions

		//bind external events
		this.bindEvents();

		//bind editor keydown events
		Event.observe(this._doc, "keydown", this.keydown.bindAsEventListener(this));

		//bind editor keyup events
		Event.observe(this._doc, "keyup", this.keypress.bindAsEventListener(this));

		//bind editor keypress events
		// Event.observe(this._doc, "keypress", this.keypress.bindAsEventListener(this));

		//bind editor focus events (used to reset designmode - Gecko bug)
		Event.observe(this._doc, "focus", this.focus.bindAsEventListener(this));


    //post-init functions

    //add event listeners to doc elements, e.g. images
    // this.listen();
	}

,

	enableDesignMode : function() {
		if (this._doc.designMode == "off") {
			this._doc.designMode = "on";
		}
		this._doc.execCommand("styleWithCSS", '', false);
	}
,
	keydown : function(e) {
	}
,
	keyup : function(e) {
	}
,
	keypress : function(e) {
		if(e.keyCode == 13 && !e.shiftKey) {
			// RETURN key
			// cleanup <br><br> between paragraphs
			// getElementsBySelector does not work in iframes
			// $(this._doc.body).getElementsBySelector('br').remove();
			// use verbose methods instead
			$A(this._doc.body.childNodes)
				.each(function(el) {
					if (el.tagName && el.tagName.toLowerCase() == 'br') {
						el.parentNode.removeChild(el);
					}
				});
		} else if (e.keyCode!=8 && e.keyCode!=46 && e.keyCode!=17 && !e.ctrlKey) {
			// NOT BACKSPACE, NOT DELETE, NOT CTRL
			// ^^ FIXES DELETION BUG ^^
		  var container = this.selected();
		
			var name = container.tagName.toLowerCase();

	    //fix forbidden main containers
	    if(
	      name == "strong" ||
	      name == "b" ||
	      name == "em" ||
	      name == "i" ||
	      name == "sub" ||
	      name == "sup" ||
	      name == "a"

	    ) name = container.parentNode.tagName.toLowerCase();
		
				
	    if(name == "body") {
				this._exec('FormatBlock', 'P');
			}
		}
		
		var ed = this;

		setTimeout(function() { ed.handleNodeChange(); }, 0);
	}
,
	focus: function(e) {
		var ed = this;
		this.enableDesignMode();
		setTimeout(function() { ed.handleNodeChange(); }, 0);
	}
,
	selected : function(type) {
		if (!type) type = 'focus';
		var sel = this._iframe.contentWindow.getSelection();
		var node = sel[type + 'Node'];
		if(node.nodeName == "#text") return(node.parentNode);
		else return(node);
	}
,
	setFocusToNode : function(node) {
		var range = document.createRange();
		range.selectNode(node);
		var selected = this._iframe.contentWindow.getSelection();
		selected.addRange(range);
		selected.collapse(node, node.childNodes.length);
		this._iframe.contentWindow.focus();
	}
};
